<template>
	<view>
		<imenu :book="book" :wd="wd" :menu="menuTree" :token="token" :result="result"
		@search="search" 
		@itemClick="itemClick"
		@tabClick="tabClick"
		@clear="clear"/>
	</view>
</template>

<script>
	import util from '../../utils/util.js'
	import config from '../../config.js'
	import api from '../../utils/api.js'

	import imenu from '../../components/menu.vue'


	export default {
		components: {
			imenu
		},
		data() {
			return {
				book: {},
				menuTree: [],
				totalMenu: [],
				result: [],
				identify: '',
				wd: '',
				token: '',
				title: '目录'
			}
		},
		onLoad: function(options) {
			let id = options.id || options.identify;
			if (!id || id == undefined) {
				if (config.debug){
					identify = 'dochub'
				}else{
					uni.redirectTo({
						url: '/pages/notfound/notfound',
					})
					return
				}
			}
			this.id = id
			this.siteId = options.siteId || 2;
			this.loadData()
		},
		onShow: function() {
			this.token = util.getToken() || ''
		},
		onShareAppMessage: function() {
			uni.showShareMenu({
				withShareTicket: true
			})
		},
		onReachBottom:function(){
			var that = this
			that.menuTree = that.menuTree.concat(that.totalMenu.splice(0, 200))
		},
		methods: {
			loadData: function() {
				let that = this
				let menu = []
				let book = {}
				util.loading()
				Promise.all([util.request(config.api.bookMenu, {
					bookId: that.id,
					siteId: that.siteId
				}), util.request(config.api.bookInfo, {
					id: that.id
				})]).then(function([resMenu, resBook]) {
					if (config.debug) console.log(resMenu, resBook)
					if (resMenu.data) {
						menu = resMenu.data.list
					}
					if (resBook.data) {
						book = resBook.data
						book.score_float = Number(book.score / 10).toFixed(1)
						book.percent = Number(book.cnt_readed / book.cnt_doc * 100).toFixed(2)
					}
				}).catch(function(e) {
					console.log(e)
				}).finally(function() {
					if (menu.length == 0) {
						uni.redirectTo({
							url: '/pages/notfound/notfound',
						})
						return
					}
					that.totalMenu = menu
					//that.menuTree = util.menuToTree(menu)
					// 分页显示
					that.menuTree = that.totalMenu.splice(0, 200)
					
					that.book =  book
					that.title = '目录 · ' + book.book_name
					uni.hideLoading()
				})
			},
			itemClick: function(e) {
				if(config.debug) console.log("itemClick", e)
				uni.navigateTo({
					url: '/pages/read/read?chapterId=' + e.id + '&siteId=' + this.siteId + "&id=" + this.id,
				})
			},
			search: function(e) {
				if(config.debug) console.log("search",e)
				util.loading("玩命搜索中...")
				let that = this
				let result = []
				util.request(config.api.searchDoc, {
					identify: that.identify,
					wd: e.wd
				}).then(function(res) {
					if (config.debug) console.log(config.api.searchDoc, res)
					if (res.data && res.data.result) {
						result = res.data.result
					}
				}).catch(function(e) {
					console.log(e)
				}).finally(function() {
					that.result = result
					that.wd = e.wd
					uni.hideLoading()
					if (result.length == 0) {
						util.toastError("没有搜索到结果")
					}
				})
			},
			clear: function() {
				if(config.debug) console.log("click clear search word")
				this.result = []
			}
		}
	}
</script>

<style>

</style>
